<template>
  <div>
    <div class="data-total flex">
      <div class="left">
        <el-form :model="params" ref="ruleForm" label-position="right" size="small" class="select-ad">
          <el-form-item label="区域" v-if="$store.state.showAgent">
            <el-select v-model="params.agentid" filterable clearable clearabledefault-first-option
                       @change="getTableList()" placeholder="--区域--">
              <el-option v-for="items in restaurants" :key="items.id" :label="items.name" :value="items.id"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div class="item-title"><span class="big">实时数据看板</span><span class="title">整点更新</span></div>
        <div class="change-chert flex-row">
          <div class="item flex" :class="{cur: itemIndex === 1}" @click="changeChertTab(1)">
            <span class="title">有效订单量</span>
            <span class="price">{{dataAll.realAmount.today|totalArr}}</span>
            <span class="z-d flex-row">比昨日
               <span class="base-up-and-down flex-row"
                     v-html='$options.filters.relatively(dataAll.realAmount.yesterday,dataAll.realAmount.today)'></span>
            </span>
            <span class="z-d flex-row">比上周同期
               <span class="base-up-and-down flex-row"
                     v-html='$options.filters.relatively(dataAll.realAmount.lastWeek,dataAll.realAmount.today)'></span>
            </span>
          </div>
          <div class="item flex" :class="{cur: itemIndex === 2}" @click="changeChertTab(2)">
            <span class="title">有效交易额</span>
            <span class="price">{{dataAll.realOrder.today|totalArr}}</span>
            <span class="z-d flex-row">比昨日
               <span class="base-up-and-down flex-row"
                     v-html='$options.filters.relatively(dataAll.realOrder.yesterday, dataAll.realOrder.today)'></span>
            </span>
            <span class="z-d flex-row">比上周同期
               <span class="base-up-and-down flex-row"
                     v-html='$options.filters.relatively(dataAll.realOrder.lastWeek, dataAll.realOrder.today)'></span>
            </span>
          </div>
          <div class="item flex" :class="{cur: itemIndex === 3}" @click="changeChertTab(3)">
            <span class="title">有单商户数</span>
            <span class="price">{{dataAll.realShop.today|totalArr}}</span>
            <span class="z-d flex-row">比昨日
               <span class="base-up-and-down flex-row"
                     v-html='$options.filters.relatively(dataAll.realShop.yesterday,dataAll.realShop.today)'></span>
            </span>
            <span class="z-d flex-row">比上周同期
               <span class="base-up-and-down flex-row"
                     v-html='$options.filters.relatively(dataAll.realShop.lastWeek,dataAll.realShop.today)'></span>
            </span>
          </div>
        </div>
        <div class="chert-tab">
          <i class="el-icon-caret-top pointer" :style="{ left: (itemIndex - 1) * 265 + 130 + 'px' }"></i>
          <div class="top"></div>
          <div id="chertLine"></div>
        </div>
        <div class="item-title"><span class="big">昨日数据看板</span></div>
        <el-row class="from-mat">
          <el-row class="list" type="flex" align="middle">
            <el-col :span="4" class="title">配送数据</el-col>
            <el-col :span="4" class="item">
              <span class="msg">取消率</span>
              <span class="data" :class="{ error: dataAll.cancellationRate > 5}">{{dataAll.cancellationRate || 0}}</span>
            </el-col>
            <el-col :span="4" class="item">
              <span class="msg">差评率</span>
              <span class="data" :class="{ error: dataAll.badRate > 5}">{{dataAll.badRate || 0}}</span>
            </el-col>
            <el-col :span="4" class="item">
              <span class="msg">超时率</span>
              <span class="data" :class="{ error: dataAll.timeoutRate > 5}">{{dataAll.timeoutRate || 0}}</span>
            </el-col>
          </el-row>
          <el-row class="list" type="flex" align="middle">
            <el-col :span="4" class="title">收支概况</el-col>
            <el-col :span="4" class="item">
              <span class="msg">抽点盈收</span>
              <span class="data">{{dataAll.pumpingRevenue || 0}}</span>
            </el-col>
            <el-col :span="4" class="item">
              <span class="msg">配送费盈收</span>
              <span class="data">{{dataAll.distributionFee || 0}}</span>
            </el-col>
            <el-col :span="4" class="item">
              <span class="msg">平台活动支出</span>
              <span class="data">{{dataAll.platformActivityExpenditure || 0}}</span>
            </el-col>
            <el-col :span="4" class="item">
              <span class="msg">商家活动支出</span>
              <span class="data">{{dataAll.merchantActivityExpenditure || 0}}</span>
            </el-col>
          </el-row>
          <el-row class="list" type="flex" align="middle">
            <el-col :span="4" class="title">昨日用户</el-col>
            <el-col :span="4" class="item">
              <span class="msg">客单价</span>
              <span class="data">{{dataAll.customerPrice || 0}}</span>
            </el-col>
            <el-col :span="4" class="item">
              <span class="msg">注册用户量</span>
              <span class="data">{{dataAll.registeredUsers || 0}}</span>
            </el-col>
            <el-col :span="4" class="item">
              <span class="msg">新用户下单量</span>
              <span class="data">{{dataAll.newUserOrderQuantity || 0}}</span>
            </el-col>
            <el-col :span="4" class="item">
              <span class="msg">新用户订单金额</span>
              <span class="data">{{dataAll.newUserOrderVolume || 0}}</span>
            </el-col>
          </el-row>
          <el-row class="list" type="flex" align="middle">
            <el-col :span="4" class="title">合作商家</el-col>
            <el-col :span="4" class="item">
              <span class="msg">全部商家</span>
              <span class="data">{{dataAll.allBusinesses || 0}}</span>
            </el-col>
            <el-col :span="4" class="item">
              <span class="msg">正在营业</span>
              <span class="data">{{dataAll.opening || 0}}</span>
            </el-col>
            <el-col :span="4" class="item">
              <span class="msg">暂停营业</span>
              <span class="data">{{dataAll.suspendBusiness || 0}}</span>
            </el-col>
            <el-col :span="4" class="item">
              <span class="msg">近3日无单商家</span>
              <span class="data">{{dataAll.noSingleMerchant || 0}}</span>
            </el-col>
          </el-row>
        </el-row>
        <div class="item-title"><span class="big">交易平台看板</span></div>
        <div class="total-price">
            <div class="title flex-row">
              <span class="msg">APP：</span>
              <span class="data"><span>{{dataAll.pie[0] || 0}}</span>元</span>
              <span class="center">| </span>
              <span class="msg">WAP：</span>
              <span class="data"><span>{{dataAll.pie[1] || 0}}</span>元</span>
            </div>
            <div id="totalOrder" class="line"></div>
        </div>
      </div>
      <div class="right">
        <el-row class="head" type="flex" align="middle" justify="space-between">
          <span class="title">昨日商户版</span>
          <!--<span class="more">查看更多<i class="el-icon-arrow-right"></i></span>-->
        </el-row>
        <ul class="item">
          <li class="flex-row title">
            <span class="sl flex-row-ver">排名</span>
            <span class="context text-over">商家名称</span>
            <span class="sr">营业额</span>
          </li>
          <li class="flex-row" v-for="(item, index) in dataAll.topList" :key="'list'+index">
            <span class="sl flex-row-ver"><i class="scort">{{index+1}}</i></span>
            <span class="context text-over">{{item.name}}</span>
            <span class="sr text-over">{{item.sale}}</span>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
  import acction from '@/utils/acction.js'
  let arrSum = (a) => {
    var s = 0
    if (a.length > 0) {
      a.forEach((item, index) => {
        s = new acction(s, (item || 0)).accAdd()
      })
      return s
    } else {
      return 0
    }
  }
  export default {
    data() {
      return {
        time: ['00:00', '1:00', '2:00', '3:00', '4:00', '6:00', '7:00', '8:00',
          '9:00', '10:00', '11:00', '12:00', '13:00', '14:00', '15:00', '16:00',
          '17:00', '18:00', '19:00', '20:00', '21:00', '22:00', '23:00'],
        params: {
          agentid: '',
        },
        itemIndex: 1, // 选中的图形
        dataAll: {
          realAmount: { // 实时数据
            today: [], // 今天24小时数据，时间没到就不传，长度数量和时间对应 00：00 是第一个
            yesterday: [], // 昨天24小时的数据，必传24个， 00：00 是第一个
            lastWeek: [] // 上周的同一星期， 24小时的数据，必传24个， 00：00 是第一个
          },
          realOrder: { // 实时订单
            today: [],
            yesterday: [],
            lastWeek: []
          },
          realShop: { // 实时有效商户
            today: [],
            yesterday: [],
            lastWeek: []
          },
          cancellationRate: 0, // 取消率
          badRate: 0, // 差评率
          timeoutRate: 0, // 超时率
          pumpingRevenue: 0, // 抽点盈收
          distributionFee: 0, // 配送费盈收
          platformActivityExpenditure: 0, // 平台活动支出
          merchantActivityExpenditure: 0, // 商家活动支出
          customerPrice: 0, // 客单价
          registeredUsers: 0, // 注册用户量 
          newUserOrderQuantity: 0, // 新用户下单量
          newUserOrderVolume: 0, // 新用户订单金额
          allBusinesses: 0, // 全部商家
          opening: 0, // 正在营业
          suspendBusiness: 0, // 暂停营业
          noSingleMerchant: 0, // 近3日无单商家
          pie: [0, 0], //APP，WAP交易额
          topList: [], //排名
        }
      }
    },
    filters: {
      totalArr(arrs) {                  //统计数组
        return arrSum(arrs)
      },
      relatively(arr1, arr2) {         //数组部分比较
        let h2 = arr2.length
        let narrs = arr1.slice(0, h2)
        let arrTotal1 = arrSum(narrs) // 昨天
        let arrTotal2 = arrSum(arr2) // 今天
        if (arrTotal1 > 0 && arrTotal2 > 0) {
          if (arrTotal1 - arrTotal2 >= 0) {
            let total = new acction(arrTotal1, arrTotal2).accSub()
            return `<span class="down">${total ? ((total / arrTotal1) * 100).toFixed(2) : 0}%</span>`
          } else {
            let total = new acction(arrTotal2, arrTotal1).accSub()
            return `<span class="up">${total ? ((total / arrTotal1) * 100).toFixed(2) : 0}%</span>`
          }
        } else if (arrTotal1 > 0 && arrTotal2 <= 0) {
          return `<span class="down">100%</span>`
        } else if (arrTotal1 <= 0 && arrTotal2 > 0) {
          return `<span class="up">100%</span>`
        } else {
          return `<span class="up">0%</span>`
        }
      }
    },
    computed: {
      restaurants() { // 区域数据
        return this.$store.state.agentList
      }
    },
    methods: {
      changeChertTab(page) {
        this.itemIndex = page
        this.drawSalePrice()
      },
      drawSalePrice() { // 图像表
        // 基于准备好的dom，初始化echarts实例
        const myChart = this.$echarts.init(document.getElementById('chertLine'))
        // 绘制图表
        let option = {
          color: ['#15d3ae', '#5ab1ee', '#f96e85'],
          legend: {data: ['今日', '昨日', '上周'], top: '10%'},
          tooltip: {trigger: 'axis'},
          xAxis: {
            type: 'category', data: this.time,
            axisLine: {show: false}, axisTick: {show: false}, boundaryGap: false
          },
          yAxis: {
            type: 'value', splitNumber: 4,
            axisLine: {show: false}, axisTick: {show: false},
            splitLine: { lineStyle: { type: 'dashed' } }
          },
          series: [{
            name: '今日', type: 'line', smooth: true,
            data: this.itemIndex === 1 ?
              this.dataAll.realAmount.today :
              this.itemIndex === 2 ? this.dataAll.realOrder.today : this.dataAll.realShop.today
          },{
            name: '昨日', type: 'line', smooth: true,
            data: this.itemIndex === 1 ?
              this.dataAll.realAmount.yesterday :
              this.itemIndex === 2 ? this.dataAll.realOrder.yesterday : this.dataAll.realShop.yesterday
          },{
            name: '上周', type: 'line', smooth: true,
            data: this.itemIndex === 1 ?
              this.dataAll.realAmount.lastWeek :
              this.itemIndex === 2 ? this.dataAll.realOrder.lastWeek : this.dataAll.realShop.lastWeek
          }]
        }
        myChart.setOption(option)
      },
      drawSaleOrder() {
        // 基于准备好的dom，初始化echarts实例
        let myChart = this.$echarts.init(document.getElementById('totalOrder'), 'macarons')
        // 绘制图表
        let option = {
          color: ['#c4b4e4', '#57d2d3'],
          tooltip: { trigger: 'item', formatter: "{a} <br/>{b} : {c} ({d}%)" },
          legend: {orient: 'vertical', left: 'left', y: 'top', data: ['APP交易', 'WAP交易']},
          series: [{
            name: '今日交易额', type: 'pie',
            data: [{
              value: this.dataAll.pie[0], name: 'APP交易'
            },{
              value: this.dataAll.pie[1], name: 'WAP交易'
            }]
          }]
        }
        myChart.setOption(option)
      },
      getTableList() { //获取数据
        this.$http.get('/server/index?id=' + this.params.agentid).then(res => {
          if (res.data.success) {
            if (res.data.data) {
              this.dataAll = res.data.data
            }
            this.drawSalePrice()
            this.drawSaleOrder()
          }
        })
      }
    },
    mounted() {
      this.getTableList()
    }
  }
</script>
<style lang="scss" scoped>
  @import "/css/index.scss";
</style>
